# -*- coding: UTF-8 -*-
__author__ = 'kennyh'

import lamp_config


HEXSETSTR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
ISHEXSET = set(HEXSETSTR)

PDU_LENGTH = {
    '\x00\x00':3,
    '\x00\x03':6,
    '\x00\x09':4,
    '\x00\x0D':35,
    '\x00\x30':12,
    '\x00\x3c':6,
    '\x00\x42':4,
    '\x02\x00':26,

    '\x03\x00':1,
    '\x03\x04':1,
    '\x03\x05':1,
    '\x03\x10':7,
    '\x03\x20':2,
    '\x03\x28':32,
    '\x04\x00':16,
    '\x04\x68':25,
    }

CMD_TYPE_TABLE = {
    'switch_cmd': 'get_switch_cmd(768',
    'dim_cmd': 'get_dim_cmd(772',
    'change_rf_channel_cmd': 'change_rf_channel_cmd(784',
    'patrol_cmd': 'get_patrol_cmd(800',
    'battery_cmd': 'get_battery_cmd(808',
    'get_battery_setting_cmd': 'get_battery_setting_cmd(808',
    'load_param_cmd': 'get_load_param_cmd(1024',
    'get_load_setting_cmd': 'get_load_setting_cmd(1024',
    'policy_cmd': 'get_policy_cmd(1128',
    'batch_switch_cmd': 'get_batch_switch_cmd(768',
    'batch_dim_cmd': 'get_batch_dim_cmd(772',
    'select_electric_supply': 'select_electric_supply(773',
    'batch_update_cmd': 'get_batch_update_cmd(-1',
    'lamp_group_cmd': 'get_lamp_group_cmd(-1',
    'set_patrol_cmd': 'set_patrol_time_cmd(-1',
    'get_patrol_cmd': 'get_patrol_time_cmd(-1',
    'set_server_address_cmd': 'set_server_address_cmd(-1',
    'get_server_address_cmd': 'get_server_address_cmd(-1'
}

broadcast_address = 'aaaaaaaa'

QUERY_PDU_LIST =[0,3,9,13,48,60,66,512]


ALARM_INFO_TYPE = {
    '0':u'过放',
    '1':u'超压',
    '2':u'负载短路',
    '3':u'电池故障',
    '4':u'内部超温',
    '5':u'外部超温',
    '6':u' ',
    '7':u'负载开路',
    '8':u'输出电容超压保护',
    '9':u' ',#u'电池板过流',
    '10':u'电池板短路',
    '11':u'电池板超压',
    '12':u' ',
    '13':u'充电逆流',
    '14':u' ',
    '15':u'锂电池低温关闭充电',
}

LORA_WAN_TYPE = 3
NBIOT_TYPE = 1
UDP_TYPE = 2
TCP_TYPE = 0

QUERY_TIME_INTERVAL = 60
CMD_TIME_INTERVAL = 1
DECODE_DATA_INTERVAL = 1
SAVE_DATA_INTERVAL = 2
HEART_CHECK_INTERVAL = 5
MYSQL_CHECK_INTERVAL = 30*60


NBIOT_ENABLE = int(lamp_config.getConfig("nbiot", "nbiot_enable"))
NBIOT_SERVER_ADDRESS    = str(lamp_config.getConfig("nbiot", "nbiot_server_addr"))
NBIOT_SERVER_PORT       = str(lamp_config.getConfig("nbiot", "nbiot_server_port"))
NBIOT_SERVER_APPID      = str(lamp_config.getConfig("nbiot", "nbiot_server_appid"))
NBIOT_SERVER_KEY        = str(lamp_config.getConfig("nbiot", "nbiot_server_appkey"))

NBIOT_SERVER_CA_FILE    = str(lamp_config.getConfig("nbiot", "nbiot_server_ca_file"))
NBIOT_SERVER_CA_KEY     = str(lamp_config.getConfig("nbiot", "nbiot_server_ca_key"))

SERVER_SUB_ADDRESS_BASE = str(lamp_config.getConfig("nbiot", "nbiot_sub_addr"))
SERVER_SUB_PORT         = str(lamp_config.getConfig("nbiot", "nbiot_sub_port"))


NBIOT_BIND_DEVICE_URL = '/rest/bindDevice/subscriber'
NBIOT_APP_CALLBACK_URL = '/rest/commandRsp/subscriber'
NBIOT_ADD_DEVICE_CALLBACK_URL = "/rest/deviceAdded/subscriber"
NBIOT_DEVICE_DATA_CHANGED_CALLBACK_URL = "/rest/deviceDataChanged/subscriber"


LORA_WAN_ENABLE         = int(lamp_config.getConfig("lorawan", "lorawanenable"))
LORAWAN_SERVER          = str(lamp_config.getConfig("lorawan", "lorawan_server_addr"))
LORAWAN_PORT            = int(lamp_config.getConfig("lorawan", "lorawan_server_port"))

HTTPS_ENABLE = 0

import ctypes
syslibc = ctypes.cdll.LoadLibrary('libc.so.6')

# System dependent, see e.g. /usr/include/x86_64-linux-gnu/asm/unistd_64.h
SYS_gettid = 186

def getThreadId():
   """Returns OS thread id - Specific to Linux"""
   return syslibc.syscall(SYS_gettid)


def get_read_length(str_pdu):
    try:
        if PDU_LENGTH.has_key(str_pdu):
            return PDU_LENGTH[str_pdu]
        else:
            return 0
    except:
        return 0

def get_read_function_name(cmd_type):
    try:
        return CMD_TYPE_TABLE[cmd_type]
    except:
        return None

def get_alarm_info(alarm_val):
    fault_list = []
    is_fault = False
    try:
        for i in range(0,16):
            cmp_val = alarm_val & (2**i)
            if cmp_val:
                fault_list.append(ALARM_INFO_TYPE[str(i)])
                is_fault = True
        str_fault = ','.join(fault_list)
    except:
        str_fault = ''
        is_fault = False
    return str_fault
